﻿using System.Windows.Forms;
using ThinkDotNetEditorLib.Models.StaticObjects;
using ThinkDotNetEditorLib.Views;

namespace ThinkDotNetEditorLib.Controllers
{
    public class StaticObjectController : ControllerBase, IStaticObjectController
    {
        private readonly IStaticObjectsView _view;

        public StaticObjectController(IStaticObjectsView view)
        {
            _view = view;
            view.MouseDown+=OnMouseDown;
            ServiceLocator.Instance.RegisterInstance(typeof(IStaticObjectController), this);
        }     
        #region Properties

        private StaticObjectInfo _staticObjectInfo;
        public StaticObjectInfo SelectedStaticObjectInfo
        {
            get { return _staticObjectInfo; }
            set
            {
                if (value == null)
                    _view.UncheckObject(_staticObjectInfo);
                _staticObjectInfo = value;
            }
        }

        #endregion

        #region Public Methods

        public StaticObjectInfo[] LoadStaticObjectInfos()
        {            
            var staticObjectLoader = ServiceLocator.Instance.Resolve<IStaticObjectInfoLoader>();
            StaticObjectInfo[] staticObjects = staticObjectLoader.Load(IO.Paths.StaticObjectsInfoFile);
            return staticObjects;
        }

        #endregion
        
        #region Events

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Right)
                SelectedStaticObjectInfo = null;
        }
        
        #endregion
    }
}
